package cn.edu.xjtu.nbrash.xbarsuite;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import cn.edu.xjtu.nbrash.xbarsuite.presms.PreSMS;

public class RashPreSMSTESTActivity extends Activity {

	private EditText etTitle;
	private EditText etContent;
	private TextView tvResut;
	private List<PreSMS> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rash_pre_smstest);

		etTitle = (EditText) findViewById(R.id.presms_title);
		etContent = (EditText) findViewById(R.id.presms_content);
		tvResut = (TextView) findViewById(R.id.presms_result);

		list = null;
		show();
	}

	public void add(View v) {
		String title = etTitle.getText().toString();
		String content = etContent.getText().toString();

		if (title != null && content != null && !title.equals("")
				&& !content.equals("")) {
			PreSMS preSMS = new PreSMS(title, content);
			PreSMS.addPreSMS(this, RashIntents.PreSMS.PRE_SMS_DATABASE, preSMS);
		}

		show();
	}

	public void update(View v) {
		String title = etTitle.getText().toString();
		String content = etContent.getText().toString();

		if (list != null && list.size() > 0 && title != null && content != null
				&& !title.equals("") && !content.equals("")) {
			PreSMS preSMS = list.get(0);
			preSMS.setContent(content);
			preSMS.setTitle(title);
			PreSMS.updatePreSMS(this, RashIntents.PreSMS.PRE_SMS_DATABASE,
					preSMS);
		}

		show();
	}

	public void delete(View v) {
		if (list != null && list.size() > 0) {
			PreSMS preSMS = list.get(0);
			PreSMS.deletePreSMS(this, RashIntents.PreSMS.PRE_SMS_DATABASE,
					preSMS);
		}

		show();
	}

	public void show() {
		list = PreSMS.getPreSMSes(this, RashIntents.PreSMS.PRE_SMS_DATABASE);

		tvResut.setText(list.toString());
	}
}
